package com.kun.service_train.controller;

import com.kun.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/service_train/map")
@CrossOrigin
public class MapController {
     @Autowired
     private RestTemplate restTemplate;
     private String ak="DIayEzVB4MZgQYWUV9QP626FFOn39IVw";
     //"x": "114.52153190" "y": "38.04831193"
     @GetMapping("getAddress")
     public R getAddress(HttpServletRequest request) throws URISyntaxException {
          String ip = request.getRemoteHost();
          System.out.println(ip);
          ip="106.119.2.34";
          String url = "http://api.map.baidu.com/location/ip?ak="+ak+"&ip="+ip+"&coor=bd09ll";
          ResponseEntity<Map> forEntity = restTemplate.getForEntity(new URI(url), Map.class);
          Map body = forEntity.getBody();
          return R.ok().data("map",body);
     }
     //创建围栏  添加
     @GetMapping("createFence/{longitude}/{latitude}")
     public R createFence(@PathVariable double longitude,@PathVariable double latitude  ) throws URISyntaxException {
          LinkedMultiValueMap<Object, Object> map = new LinkedMultiValueMap<>();
          String url = "http://yingyan.baidu.com/api/v3/fence/createcirclefence";
          map.add("ak",ak);
          map.add("service_id",230329);
          map.add("longitude",longitude);
          map.add("latitude",latitude);
          map.add("radius",300);
          map.add("coord_type","bd09ll");
          ResponseEntity<String> post = restTemplate.postForEntity(new URI(url), map, String.class);

          return R.ok().data("post",post.getBody());
     }

     //鹰眼围栏   查询
     @GetMapping("/queryFence")
     public R queryFence(@Param("ids") String ids) throws URISyntaxException {
          String url = "http://yingyan.baidu.com/api/v3/fence/list?ak="+ak;
          url+="&service_id=230329";
          url+="&fence_ids="+ids;
          ResponseEntity<String> entity = restTemplate.getForEntity(new URI(url), String.class);
          return R.ok().data("entity",entity.getBody());
     }
     // 删除围栏
     @GetMapping("delFence/{ids}")
     public R delFence(@PathVariable String ids) throws URISyntaxException {
          String url = "http://yingyan.baidu.com/api/v3/fence/delete";
          LinkedMultiValueMap<Object, Object> map = new LinkedMultiValueMap<>();
          map.add("ak",ak);
          map.add("service_id",230329);
          map.add("fence_ids",ids);
          ResponseEntity<String> post = restTemplate.postForEntity(new URI(url), map, String.class);

          return R.ok().data("post",post.getBody());
     }


     //添加终端
     @GetMapping("createEntity/{entity_name}")
     public R createEntity(@PathVariable String entity_name) throws URISyntaxException {
          String url = "http://yingyan.baidu.com/api/v3/entity/add";
          LinkedMultiValueMap<Object, Object> map = new LinkedMultiValueMap<>();
          map.add("ak",ak);
          map.add("service_id",230329);
          map.add("entity_name",entity_name);
          ResponseEntity<String> post = restTemplate.postForEntity(new URI(url), map, String.class);
          return R.ok().data("post",post.getBody());

     }
     //围栏添加终端
     @GetMapping("add_fence_entity/{fenceId}/{name}")
     public R add_fence_entity(@PathVariable int fenceId,@PathVariable String name) throws URISyntaxException {
          String url = "http://yingyan.baidu.com/api/v3/fence/addmonitoredperson";
          LinkedMultiValueMap<Object, Object> map = new LinkedMultiValueMap<>();
          map.add("ak",ak);
          map.add("service_id",230329);
          map.add("fence_id",fenceId);
          map.add("monitored_person",name);
          ResponseEntity<String> post = restTemplate.postForEntity(new URI(url), map, String.class);
          return R.ok().data("post",post.getBody());

     }
     //定位终端 上传轨迹点
     @GetMapping("addPoint/{longitude}/{latitude}/{name}")
     public R addPoint(@PathVariable double longitude,@PathVariable double latitude,@PathVariable String name) throws URISyntaxException {
          String url = "http://yingyan.baidu.com/api/v3/track/addpoint";
          LinkedMultiValueMap<Object, Object> map = new LinkedMultiValueMap<>();
          map.add("ak",ak);
          map.add("service_id",230329);
          map.add("entity_name",name);
          map.add("longitude",longitude);
          map.add("latitude",latitude);
          map.add("loc_time", (long)(new Date().getTime()/1000));
          map.add("coord_type_input","bd09ll");
          ResponseEntity<String> post = restTemplate.postForEntity(new URI(url), map, String.class);
          return R.ok().data("post",post.getBody());

     }
     //判断终端是否在围栏内
     @GetMapping("/queryStatus/{name}")
     public R queryStatus(@PathVariable String name) throws URISyntaxException {
          String url="http://yingyan.baidu.com/api/v3/fence/querystatus?ak="+ak;
          url+="&service_id=230329";
          url +="&monitored_person="+name;
          ResponseEntity<String> entity = restTemplate.getForEntity(new URI(url), String.class);
          return R.ok().data("entity",entity.getBody());
     }
}
